﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class searchInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string type = Request["type"];
            string text = Request["text"];
            string sqlSel = "select * from tb_musicInfo where " + type + " like  '%" + text + "%'";
            gvMusic.DataSource = dataoperate.getRows(sqlSel);
          
            gvMusic.DataKeyNames = new string[] { "id" };
            gvMusic.DataBind();
            labNum.Text = gvMusic.Rows.Count.ToString();
            bindAudition();
        }
    }







    protected void gvMusic_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //判断是否为下载操作
        if (e.CommandName == "down")
        {
            //获取歌曲的id 
            string id = e.CommandArgument.ToString();
            //创建SQL语句，查询歌曲的详细信息
            string sqlSel = "select musicPath from tb_musicInfo where id=" + id;
            //创建SQL语句，更新歌曲的下载次数
            string sqlUpdate = "update tb_musicInfo set downSum=downSum+1 where id=" + id; ;
            //执行SQL语句
            dataoperate.execSql(sqlUpdate);
            //调用自定义downFile方法实现歌曲的下载操作
            downFile(dataoperate.getTier(sqlSel));

        }
    }

    protected void downFile(string musicPath)
    {
        string path = Server.MapPath("musicFile/") + musicPath;
        //初始化 FileInfo 类的实例，它作为文件路径的包装
        FileInfo fi = new FileInfo(path);
        //判断文件是否存在
        if (fi.Exists)
        {
            //将文件保存到本机上
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fi.Name));
            Response.AddHeader("Content-Length", fi.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.Filter.Close();
            Response.WriteFile(fi.FullName);
            Response.End();
        }
    }


    //绑定GridView控件显示试听排行榜
    protected void bindAudition()
    {
        string sqlSel = "select top 10 * from tb_musicInfo order by auditionSum desc";
        gvAudition.DataSource = dataoperate.getRows(sqlSel);
        gvAudition.DataKeyNames = new string[] { "id" };
        gvAudition.DataBind();
    }



    protected void imgBtnHaving_Click(object sender, ImageClickEventArgs e)
    {
        //判断GridView控件中的复选框状态
        if (isCheck(gvMusic))
        {
            //遍历GridView控件
            foreach (GridViewRow gvr in gvMusic.Rows)
            {
                //将GridView控件中的复选框设置为选中状态
                ((CheckBox)gvr.FindControl("CheckBox1")).Checked = false;
            }
        }
        else
        {
            //遍历GridView控件
            foreach (GridViewRow gvr in gvMusic.Rows)
            {
                //将GridView控件中的复选框设置为不选中状态
                ((CheckBox)gvr.FindControl("CheckBox1")).Checked = true;
            }
        }
    }
    protected void imgBtnReverse_Click(object sender, ImageClickEventArgs e)
    {
        foreach (GridViewRow gvr in gvMusic.Rows)
        {
            ((CheckBox)gvr.FindControl("CheckBox1")).Checked = !((CheckBox)gvr.FindControl("CheckBox1")).Checked;
        }
    }


    //判断GridView控件中是否拥有选择的复选框
    protected bool isCheck(GridView gv)
    {
        bool c = false;
        //遍历GridView控件
        foreach (GridViewRow gvr in gv.Rows)
        {
            //判断复选框状态是否选中
            if (((CheckBox)gvr.FindControl("CheckBox1")).Checked)
            {
                c = true;
                break;
            }
        }
        return c;
    }


    protected void imgBtnPlay_Click(object sender, ImageClickEventArgs e)
    {
        int j = 0;
        if (isCheck(gvMusic))
        {
            string musicList = "";
            for (int i = 0; i < gvMusic.Rows.Count; i++)
            {
                if (((CheckBox)gvMusic.Rows[i].FindControl("CheckBox1")).Checked)
                {
                    musicList += gvMusic.DataKeys[i].Value.ToString() + ",";
                }
            }

            Response.Write("<script>window.open('playListMusic.aspx?id=" + musicList + "','','width=440,height=260')</script>");
        }
        else
        {
            RegisterStartupScript("", "<script>alert('请选择需要播放的歌曲！')</script>");
        }
    }
    protected void imgBtnAPlay_Click(object sender, ImageClickEventArgs e)
    {
        int j = 0;
        if (isCheck(gvAudition))
        {
            string musicList = "";
            for (int i = 0; i < gvAudition.Rows.Count; i++)
            {
                if (((CheckBox)gvAudition.Rows[i].FindControl("CheckBox1")).Checked)
                {
                    musicList += gvAudition.DataKeys[i].Value.ToString() + ",";
                }
            }

            Response.Write("<script>window.open('playListMusic.aspx?id=" + musicList + "','','width=440,height=260')</script>");
        }
        else
        {
            RegisterStartupScript("", "<script>alert('请选择需要播放的歌曲！')</script>");
        }


    }
    protected void imgBtnAFull_Click(object sender, ImageClickEventArgs e)
    {
        //判断GridView控件中的复选框状态
        if (isCheck(gvAudition))
        {
            //遍历GridView控件
            foreach (GridViewRow gvr in gvAudition.Rows)
            {
                //将GridView控件中的复选框设置为选中状态
                ((CheckBox)gvr.FindControl("CheckBox1")).Checked = false;
            }
        }
        else
        {
            //遍历GridView控件
            foreach (GridViewRow gvr in gvAudition.Rows)
            {
                //将GridView控件中的复选框设置为不选中状态
                ((CheckBox)gvr.FindControl("CheckBox1")).Checked = true;
            }
        }
    }
}
